package tcln.sors.client.core.mvc;

import java.util.ArrayList;
import java.util.List;

/**
 * A model data containing a list of child models.
 * 
 * @param <CM>
 *            Child model type.
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class ListModelData<CM extends ModelData<?>> extends DefaultModelData<ListModelData<?>> {
    private static final String PROPERTY_CHILD_MODELS = "childModels";

    /**
     * Constructs this model data with reserved place for 20 child models.
     */
    public ListModelData() {
        doConstruct(20);
    }

    /**
     * Constructs this model data.
     * 
     * @param initialSize
     *            Initial child model size.
     */
    public ListModelData(int initialSize) {
        doConstruct(initialSize);
    }

    /**
     * Returns current value of {@link #childModels}.
     * 
     * @return the {@link #childModels}.
     */
    @SuppressWarnings("unchecked")
    public List<CM> getChildModels() {
        return get(PROPERTY_CHILD_MODELS, List.class);
    }

    /**
     * Sets new value for {@link #childModels}.
     * 
     * @param childModels
     *            the new childModels to set
     */
    public void setChildModels(List<CM> childModels) {
        set(PROPERTY_CHILD_MODELS, childModels);
    }

    private void doConstruct(int initialSize) {
        this.propertyChangedMonitored = false;
        set(PROPERTY_CHILD_MODELS, new ArrayList<CM>(initialSize));
        this.propertyChangedMonitored = true;
    }
}
